package com.atguigu.edu.realtime.publisher.controller;

import com.atguigu.edu.realtime.publisher.bean.UserPageCt;
import com.atguigu.edu.realtime.publisher.service.UserUserActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.atguigu.edu.realtime.publisher.util.DateUtil;

import java.math.BigDecimal;
import java.util.List;

@RestController
public class UserUserActionController {
    @Autowired
    UserUserActionService userUserActionService;

    @RequestMapping("/back")
    public String getBackUser(@RequestParam(value = "date", defaultValue = "1") Integer date){
        if(date == null){
            DateUtil.now();
        }
        BigDecimal backUser = userUserActionService.getBackUser(date);
        String json = "{\"status\": 0,\"data\": " + backUser + "}";
        return json;
    }
    @RequestMapping("/active")
    public String getActiveUser(@RequestParam(value = "date", defaultValue = "1") Integer date){
        if(date == null){
            DateUtil.now();
        }
        BigDecimal activeUser = userUserActionService.getActiveUser(date);
        String json = "{\"status\": 0,\"data\": " + activeUser + "}";
        return json;
    }
    @RequestMapping("/new")
    public String getNewUser(@RequestParam(value = "date", defaultValue = "1") Integer date){
        if(date == null){
            DateUtil.now();
        }
        BigDecimal newUser = userUserActionService.getNewUser(date);
        String json = "{\"status\": 0,\"data\": " + newUser + "}";
        return json;
    }
    @RequestMapping("uvbypage")
    public String getUvByPage(@RequestParam(value = "date", defaultValue = "1") Integer date){
        if(date == null){
            DateUtil.now();
        }
        List<UserPageCt> uvByPage = userUserActionService.getUvByPage(date);
        StringBuilder sb = new StringBuilder("{\"status\": 0,\"msg\": \"\",\"data\": [");
        for (int i = 0; i< uvByPage.size() ;i++){
            UserPageCt userPageCt = uvByPage.get(i);
            sb.append("{ \"name\": \""+ userPageCt.getPageId() +"\", \"value\": \""+userPageCt.getUvCt()+"\" }");
            if(i< uvByPage.size() -1){
                sb.append(",");
            }
        }
        sb.append(" ]}");
        return sb.toString();
    }

}
